package com.dd.cloud.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.cloud.user.entity.classroom.MediaClassify;
import com.dd.cloud.user.req.classroom.*;
import com.dd.cloud.user.res.classroom.MediaClassifyRes;
import com.dd.cloud.user.res.classroom.MediaContentDetailRes;
import com.dd.cloud.user.res.classroom.MediaSectionPageRes;
import com.dd.cloud.user.service.classroom.IMediaClassifyService;
import com.dd.cloud.user.service.classroom.IMediaContentService;
import com.dd.cloud.user.service.classroom.IMediaPlaySiteService;
import com.dd.cloud.user.service.classroom.IMediaSectionService;
import com.dd.cloud.common.utils.JsonUtils;
import com.dd.cloud.common.utils.ValidatorUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 多媒体内容 控制器
 * </p>
 *
 * @author czg
 * @since 2022-04-27
 */
@RestController
@RequestMapping("/classroom")
@Validated
@Slf4j
public class ClassroomController {
    @Autowired
    private IMediaSectionService mediaSectionService;
    @Autowired
    private IMediaContentService mediaContentService;
    @Autowired
    private IMediaClassifyService mediaClassifyService;
    @Autowired
    private IMediaPlaySiteService mediaPlaySiteService;

    /**
     * 查询所有类型
     *
     * @return 科室列表
     */
    @GetMapping("/section/classify")
    public ResponseEntity<List<MediaClassify>> getSectionClassify() {
        List<MediaClassify> list = mediaClassifyService.getMediaClassifyList();
        return ResponseEntity.ok(list);
    }

    /**
     * 分页查询所有类型
     *
     * @return 科室列表
     */
    @GetMapping("/section/classify/page")
    public ResponseEntity<Page<MediaClassifyRes>> getSectionClassify(@Valid QueryMediaClassifyReq req) {
        Page<MediaClassifyRes> list = mediaClassifyService.getMediaClassifyPage(req);
        return ResponseEntity.ok(list);
    }

    /**
     * 添加云课堂类型
     *
     * @return 科室列表
     */
    @PostMapping("/section/classify/add")
    public ResponseEntity<Void> addSectionClassify(@Valid CreateMediaClassifyReq req) {
        mediaClassifyService.addMediaClassify(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 编辑云课堂类型
     *
     * @return 科室列表
     */
    @PostMapping("/section/classify/update")
    public ResponseEntity<Void> updateSectionClassify(@Valid UpdateMediaClassifyReq req) {
        mediaClassifyService.updateMediaClassify(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 编辑云课堂类型状态
     *
     * @return 科室列表
     */
    @PostMapping("/section/classify/update/status")
    public ResponseEntity<Void> updateSectionClassifyStatus(@NotNull(message = "id不能为空") Integer id,
                                                            @NotNull(message = "状态不能为空") Integer status) {
        mediaClassifyService.updateMediaClassifyStatus(id, status);
        return ResponseEntity.ok().build();
    }

    /**
     * 分页查询视频小节列表
     * @param req
     * @return
     */
    @GetMapping("/section/page")
    public ResponseEntity<Page<MediaSectionPageRes>> getSectionPage(@Valid MediaSectionPageReq req){
        Page<MediaSectionPageRes> page = mediaSectionService.getSectionPage(req);
        return ResponseEntity.ok(page);
    }

    /**
     * 查询视频详情
     * @param id
     * @return
     */
    @GetMapping("/section/detail")
    public ResponseEntity<MediaSectionPageRes> getSectionDetail(@NotNull(message = "id不能为空") Integer id){
        MediaSectionPageRes res = mediaSectionService.getMediaSectionDetail(id);
        return ResponseEntity.ok(res);
    }

    /**
     * 添加视频小节
     * @param req
     * @return
     */
    @PostMapping("/section/add")
    public ResponseEntity<Void> addSection(@Valid CreateMediaSectionReq req){
        mediaSectionService.createSection(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改视频小节
     * @param req valid判断是否为验证请求 0验证,只验证不操作, 1操作 由于文件上传量大慢, 需要先验证数据是否合规
     * @return
     */
    @PostMapping("/section/update")
    public ResponseEntity<Void> updateSection(//@Valid UpdateMediaSectionReq req
                                              @RequestPart MultipartFile[] files,
                                              @RequestParam(value = "req") String req){
        log.info("修改视频小节,{}", req);
        UpdateMediaSectionReq jsonReq = JsonUtils.strToObject(req, UpdateMediaSectionReq.class);
        ValidatorUtils.verify(jsonReq, UpdateMediaSectionReq.class);
        //ValidatorUtils.verify(jsonReq.getContentList(), ValidList.class);

        mediaSectionService.updateSection(jsonReq, files);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改视频小节状态
     * @param id
     * @return
     */
    @PostMapping("/section/update/status")
    public ResponseEntity<Void> updateSectionStatus(@NotNull(message = "id不能为空") Integer id,
                                                    @NotNull(message = "状态不能为空") Integer status){
        mediaSectionService.updateSectionStatus(id, status);
        return ResponseEntity.ok().build();
    }

    /**
     * 文档导入视频小节
     * @param file
     * @return
     */
    @PostMapping("/section/import")
    public ResponseEntity<Void> getSectionPage(MultipartFile file){
        mediaSectionService.importSection(file);
        return ResponseEntity.ok().build();
    }

    /**
     * 批量上传视频(只能同小节导入)
     * @Param req valid判断是否为验证请求 0验证,只验证不操作, 1操作 由于文件上传量大慢, 需要先验证数据是否合规
     * @return
     */
    @PostMapping(value = "/content/upload/batch")
    public ResponseEntity<Void> contentUploadBatch(@RequestPart MultipartFile[] files,
                                                   @RequestParam(value = "req") String req
                                                   ){
        log.info("批量导入,{}", req);
        UploadMediaContentReq jsonReq = JsonUtils.strToObject(req, UploadMediaContentReq.class);
        ValidatorUtils.verify(jsonReq, UploadMediaContentReq.class);
        //ValidatorUtils.verify(jsonReq.getMediaList(), ValidList.class);
        mediaSectionService.uploadMediaList(jsonReq, files);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加小节播放次数
     * @param id
     * @return
     */
    @PostMapping("/section/plays/add")
    public ResponseEntity<Page<MediaSectionPageRes>> sectionPlaysAdd(@NotNull(message = "小节id不能为空") Integer id){
        mediaSectionService.addSectionPlays(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询小节所有视频
     * @param sectionId
     * @return
     */
    @GetMapping("/content/list")
    public ResponseEntity<List<MediaContentDetailRes>> getContentList(@NotNull(message = "小节id不能为空") Integer sectionId){
        List<MediaContentDetailRes> list = mediaContentService.getMediaContentList(sectionId, null);

        return ResponseEntity.ok(list);
    }

    /**
     * 设置视频最后一次播放位置
     * @param id
     * @return
     */
    @PostMapping("/content/set/playSite")
    public ResponseEntity<Page<MediaSectionPageRes>> contentSetPlaySite(@NotNull(message = "视频id不能为空") Integer id,
                                                                        @NotNull(message = "最后播放位置不能为空") Integer playSite,
                                                                        Long duration){
        mediaPlaySiteService.setMediaContentPlaySite(id, playSite, duration);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改视频小节状态
     * @param id
     * @return
     */
    @PostMapping("/content/update/status")
    public ResponseEntity<Void> updateContentStatus(@NotNull(message = "id不能为空") Integer id,
                                                    @NotNull(message = "状态不能为空") Integer status){
        mediaContentService.updateContentStatus(id, status);
        return ResponseEntity.ok().build();
    }

}

